package com.ht.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.ht.data.Result;
import com.ht.domain.bo.*;
import com.ht.domain.po.TbKnowledge;
import com.ht.domain.vo.*;
import com.ht.dto.UserDTO;
import com.ht.em.ResultEnum;
import com.ht.feign.AppointmentRegisteredFeign;
import com.ht.service.ArticleService;
import com.ht.service.KnowledgeService;
import com.ht.service.UserCollectService;
import com.ht.util.JwtUtil;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 知识模块相关查询
 */
@RestController
@RequestMapping("knowledge")
@Api
@RefreshScope //开启自动刷新配置
public class KnowledgeController {

    //知识表
    @Autowired
    private KnowledgeService knowledgeService;

    //用户收藏表
    @Autowired
    private UserCollectService userCollectService;

    @Autowired
    private ArticleService articleService;

    @Autowired
    private AppointmentRegisteredFeign appointmentRegisteredFeign;


    @ApiOperation("查询所有省")
    @GetMapping("findProvinceAll")
    public Result findProvinceAll(){
        return appointmentRegisteredFeign.queryByProvince();
    }

    @ApiOperation("根据省id查询对应的市")
    @ApiImplicitParam(name = "provinceId",value = "省id",required = true,paramType = "path",dataTypeClass = Long.class)
    @GetMapping("findCityByProvinceId/{provinceId}")
    public Result findCityByProvinceId(@PathVariable("provinceId")Long provinceId){
        TbKnowledge tbKnowledge = new TbKnowledge();
        return appointmentRegisteredFeign.queryByCity(provinceId);
    }

    /**
     * 根据知识类别id查询前八条数据
     * @param categoryId 知识id
     * @param chargeStatus 是否收费
     * @return
     */
    @ApiOperation("根据知识类别id查询前八条数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "categoryId",value = "知识对应的类别表id",required = true,paramType = "path",dataTypeClass = Long.class),
            @ApiImplicitParam(name = "chargeStatus",value = "是否收费, 0:未定,1:自费,2:免费,可不传",required = false,paramType = "query",dataTypeClass = Integer.class)
    })

    @GetMapping("findListByCategoryId/{categoryId}")
    public Result findListByCategoryId(@PathVariable("categoryId")Long categoryId,Integer chargeStatus){
        List<TbKnowledgeBO> tbKnowledgeBOList = knowledgeService.findListByCategoryId(categoryId,chargeStatus);
        //判断是否查询到数据,如果没有数据返回状态码(未找到符合条件的数据)
        if (tbKnowledgeBOList.size() <= 0){
            return Result.response(ResultEnum.QUERY_NULL);
        }
        //将bo类转成vo类返回数据到前台
        List<TbKnowledgeVO> tbKnowledgeVOList = new ArrayList<>();
        tbKnowledgeBOList.forEach((tbKnowledgeBO) -> {
            TbKnowledgeVO tbKnowledgeVO = new TbKnowledgeVO(tbKnowledgeBO);
            tbKnowledgeVOList.add(tbKnowledgeVO);
        });
        return Result.OK().setData(tbKnowledgeVOList);
    }

    /**
     * 根据疾病,疫苗,检查,治疗名称进行模糊查询
     * @param knowledgeName 名称
     * @return
     */
    @ApiOperation("根据疾病,疫苗,检查,治疗名称进行模糊查询")
    @ApiImplicitParam(name = "knowledgeName",value = "需要搜索的名称",required = true,paramType = "query",dataTypeClass = String.class)
    @PostMapping("findListByKnowledgeName")
    public Result findListByName(@RequestParam("knowledgeName") String knowledgeName){
        List<TbKnowledgeBO> tbKnowledgeBOList = knowledgeService.findListByName(knowledgeName);
        //判断是否查询到数据,如果没有数据返回状态码(未找到符合条件的数据)
        if (tbKnowledgeBOList.size() <= 0){
            return Result.response(ResultEnum.QUERY_NULL);
        }
        //将bo类转成vo类返回数据到前台
        List<TbKnowledgeVO> tbKnowledgeVOList = new ArrayList<>();
        tbKnowledgeBOList.forEach((tbKnowledgeBO) -> {
            TbKnowledgeVO tbKnowledgeVO = new TbKnowledgeVO(tbKnowledgeBO);
            tbKnowledgeVOList.add(tbKnowledgeVO);
        });
        return Result.OK().setData(tbKnowledgeVOList);
    }

    /**
     * 根据专科id查询对应的知识
     * @param juniorId 专科id
     * @return
     */
    @ApiOperation("根据专科id查询对应的知识")
    @ApiImplicitParam(name = "juniorId",value = "专科id",required = true,paramType = "path",dataTypeClass = Long.class)
    @GetMapping("findListByJuniorId/{juniorId}")
    public Result findListByJuniorId(@PathVariable("juniorId")Long juniorId){
        List<TbKnowledgeBO> knowledgeBOList = knowledgeService.findListByJuniorId(juniorId);
        //判断是否查询到数据,如果没有数据返回状态码(未找到符合条件的数据)
        if (knowledgeBOList.size() <= 0){
            return Result.response(ResultEnum.QUERY_NULL);
        }
        //将bo类转成vo类返回数据到前台
        List<TbKnowledgeVO> knowledgeVOList = new ArrayList<>();
        knowledgeBOList.forEach((knowledgeBO) -> {
            TbKnowledgeVO knowledgeVO = new TbKnowledgeVO(knowledgeBO);
            knowledgeVOList.add(knowledgeVO);
        });
        return Result.OK().setData(knowledgeVOList);
    }

    /**
     * 根据知识id和市id还有医院等级,医院类型推荐医院
     * @param knowledgeId 知识id
     * @param cityId 市id
     * @param hospitalGrade 医院等级
     * @param hospitalTypeId 医院类型
     * @return
     */
    @ApiOperation("根据知识id和市id还有医院等级,医院类型推荐医院")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "knowledgeId",value = "知识id",required = true,paramType = "query",dataTypeClass = Long.class),
            @ApiImplicitParam(name = "cityId",value = "市id",required = false,paramType = "query",dataTypeClass = Long.class),
            @ApiImplicitParam(name = "hospitalGrade",value = "医院等级",required = false,paramType = "query",dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "hospitalTypeId",value = "医院类型",required = false,paramType = "query",dataTypeClass = Long.class),
    })
    @PostMapping("findHospitalListByKnowledgeId")
    public Result findHospitalListByKnowledgeId(@RequestParam("knowledgeId")Long knowledgeId,Long cityId,Integer hospitalGrade,Long hospitalTypeId){
        List<TbHospitalBO> hospitalBOList =  knowledgeService.findHospitalListByKnowledgeId(knowledgeId,cityId,hospitalGrade,hospitalTypeId);
        //判断是否查询到数据,如果没有数据返回状态码(未找到符合条件的数据)
        if (hospitalBOList.size() <= 0){
            return Result.response(ResultEnum.QUERY_NULL);
        }
        //将bo类转成vo类返回数据到前台
        List<TbHospitalVO> hospitalVOList = new ArrayList<>();
        hospitalBOList.forEach((hospitalBO) -> {
            TbHospitalVO hospitalVO = new TbHospitalVO();
            BeanUtils.copyProperties(hospitalBO,hospitalVO);
            hospitalVOList.add(hospitalVO);
        });
        return Result.OK().setData(hospitalVOList);
    }

    /**
     *根据知识id,市id,职位id,医院等级,科室id,医生坐诊日期推荐医生
     * @param knowledgeId 知识id
     * @param cityId 市id
     * @param levelId 职位id
     * @param hospitalGrade 医院等级
     * @param departmentId 科室id
     * @param workDate 医生坐诊日期
     * @return
     */
    @ApiOperation("根据知识id,市id,职位id,医院等级,科室id,医生坐诊日期推荐医生")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "knowledgeId",value = "知识id",required = true,paramType = "query",dataTypeClass = Long.class),
            @ApiImplicitParam(name = "cityId",value = "市id",required = false,paramType = "query",dataTypeClass = Long.class),
            @ApiImplicitParam(name = "levelId",value = "职位id",required = false,paramType = "query",dataTypeClass = Long.class),
            @ApiImplicitParam(name = "hospitalGrade",value = "医院等级",required = false,paramType = "query",dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "departmentId",value = "科室id",required = false,paramType = "query",dataTypeClass = Long.class),
            @ApiImplicitParam(name = "workDate",value = "医生坐诊日期",required = false,paramType = "query",dataTypeClass = Long.class)
    })
    @PostMapping("findDoctorListByKnowledgeId")
    public Result findDoctorListByKnowledgeId(@RequestParam("knowledgeId")Long knowledgeId,Long cityId,Long levelId,Integer hospitalGrade,Long departmentId,String workDate){
        List<TbDoctorBO> doctorBOList =  knowledgeService.findDoctorListByKnowledgeId(knowledgeId,cityId,levelId,hospitalGrade,departmentId,workDate);
        //判断是否查询到数据,如果没有数据返回状态码(未找到符合条件的数据)
        if (doctorBOList.size() <= 0){
            return Result.response(ResultEnum.QUERY_NULL);
        }
        //将bo类转成vo类返回数据到前台
        List<TbDoctorVO> doctorVOList = new ArrayList<>();
        doctorBOList.forEach((doctorBO) -> {
            TbDoctorVO doctorVO = new TbDoctorVO();
            BeanUtils.copyProperties(doctorBO,doctorVO);
            doctorVOList.add(doctorVO);
        });
        return Result.OK().setData(doctorVOList);
    }

    /**
     * 根据医院id和知识id查询医生
     * @param knowledgeId 知识id
     * @param hospitalId 医院id
     * @return
     */
    @ApiOperation("根据医院id和知识id查询医生")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "knowledgeId",value = "知识id",required = true,paramType = "query",dataTypeClass = Long.class),
            @ApiImplicitParam(name = "hospitalId",value = "医院id",required = true,paramType = "query",dataTypeClass = Long.class)
    })
    @PostMapping("findDoctorListByHospitalIdAndknowledgeId")
    public Result findDoctorListByHospitalIdAndknowledgeId(@RequestParam("knowledgeId")Long knowledgeId,@RequestParam("hospitalId")Long hospitalId){
        List<TbDoctorBO> doctorBOList = knowledgeService.findDoctorListByHospitalIdAndknowledgeId(knowledgeId,hospitalId);
        //判断是否查询到数据,如果没有数据返回状态码(未找到符合条件的数据)
        if (doctorBOList.size() <= 0){
            return Result.response(ResultEnum.QUERY_NULL);
        }
        //将bo类转成vo类返回数据到前台
        List<TbDoctorVO> doctorVOList = new ArrayList<>();
        doctorBOList.forEach((doctorBO) -> {
            TbDoctorVO doctorVO = new TbDoctorVO();
            BeanUtils.copyProperties(doctorBO,doctorVO);
            doctorVOList.add(doctorVO);
        });
        return Result.OK().setData(doctorVOList);
    }


    /**
     * 根据知识id查询知识相关信息
     * @param knowledgeId 知识id
     * @return
     */
    @ApiOperation("根据知识id查询知识相关信息")
    @ApiImplicitParam(name = "knowledgeId",value = "知识id",required = true,paramType = "path",dataTypeClass = Long.class)
    @GetMapping("findRelevantListByKnowledgeId/{knowledgeId}")
    public Result findRelevantListByKnowledgeId(@PathVariable("knowledgeId")Long knowledgeId){
        List<TbRelevantBO> relevantBOList = knowledgeService.findRelevantListByKnowledgeId(knowledgeId);
        //判断是否查询到数据,如果没有数据返回状态码(未找到符合条件的数据)
        if (relevantBOList.size() <= 0){
            return Result.response(ResultEnum.QUERY_NULL);
        }
        //将bo类转成vo类返回数据到前台
        List<TbRelevantVO> relevantVOList = new ArrayList<>();
        relevantBOList.forEach((relevantBO) -> {
            TbRelevantVO relevantVO = new TbRelevantVO();
            BeanUtils.copyProperties(relevantBO,relevantVO);
            relevantVOList.add(relevantVO);
        });
        return Result.OK().setData(relevantVOList);
    }

    /**
     * 用户收藏
     * @param knowledgeId 知识id
     * @return
     */
    @ApiOperation("用户收藏")
    @ApiImplicitParam(name = "knowledgeId",value = "知识id",paramType = "path",required = true,dataTypeClass = Long.class)
    @PostMapping("addUserCollect/{knowledgeId}")
    public Result addUserCollect(@PathVariable("knowledgeId")Long knowledgeId,@RequestHeader("AuthToken")String token){

        Claims claims = new JwtUtil().parseJwt(token);
        //取出用户数据
        UserDTO userDTO = JSONUtil.toBean(claims.getSubject(), UserDTO.class);
        Integer i = userCollectService.addUserCollect(knowledgeId,userDTO.getId());
        if (i <= 0){
            return Result.ERROR();
        }
        return Result.OK();
    }

    /**
     * 查询用户的收藏取前二十条
     * @param userId
     * @return
     */
    @ApiOperation("查询用户的收藏取前二十条")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId",value = "id",paramType = "query",required = true,dataTypeClass = Long.class)
    })
    @PostMapping("findCollectListTwentyByUserId")
    public Result findCollectListTwentyByUserId(@RequestParam("userId")Long userId){
        List<UserCollectBO> userCollectBOList = userCollectService.findCollectListTwentyByUserId(userId);
        //判断是否查询到数据,如果没有数据返回状态码(未找到符合条件的数据)
        if (userCollectBOList.size() <= 0){
            return Result.response(ResultEnum.QUERY_NULL);
        }
        //将bo类转成vo类返回数据到前台
        List<UserCollectVO> userCollectVOList = new ArrayList<>();
        userCollectBOList.forEach((userCollectBO) -> {
            UserCollectVO userCollectVO = new UserCollectVO();
            BeanUtils.copyProperties(userCollectBO,userCollectVO);
            userCollectVOList.add(userCollectVO);
        });
        return Result.OK().setData(userCollectVOList);
    }

    /**
     * 根据用户id查询用户的全部收藏
     * @param userId
     * @return
     */
    @ApiOperation("根据用户id查询用户的全部收藏")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId",value = "id",paramType = "query",required = true,dataTypeClass = Long.class)
    })
    @PostMapping("findCollectListByUserId")
    public Result findCollectListByUserId(@RequestParam("userId")Long userId){
        List<UserCollectBO> userCollectBOList = userCollectService.findCollectListByUserId(userId);
        //判断是否查询到数据,如果没有数据返回状态码(未找到符合条件的数据)
        if (userCollectBOList.size() <= 0){
            return Result.response(ResultEnum.QUERY_NULL);
        }
        //将bo类转成vo类返回数据到前台
        List<UserCollectVO> userCollectVOList = new ArrayList<>();
        userCollectBOList.forEach((userCollectBO) -> {
            UserCollectVO userCollectVO = new UserCollectVO();
            BeanUtils.copyProperties(userCollectBO,userCollectVO);
            userCollectVOList.add(userCollectVO);
        });
        return Result.OK().setData(userCollectVOList);
    }

    /**
     * 根据知识id查询相关科普文章
     * @param knowledgeId
     * @return
     */
    @ApiOperation("根据知识id查询相关科普文章")
    @ApiImplicitParam(name = "knowledgeId",value = "知识id",paramType = "path",required = true,dataTypeClass = Long.class)
    @GetMapping("findArticleListByknowledgeId/{knowledgeId}")
    public Result findArticleListByknowledgeId(@PathVariable("knowledgeId")Long knowledgeId){
        List<ArticleBO> articleBOList = articleService.findArticleListByknowledgeId(knowledgeId);
        //判断是否查询到数据,如果没有数据返回状态码(未找到符合条件的数据)
        if (articleBOList.size() <= 0){
            return Result.response(ResultEnum.QUERY_NULL);
        }
        //将bo类转成vo类返回数据到前台
        List<ArticleVO> articleVOList = new ArrayList<>();
        articleBOList.forEach((articleBO) -> {
            ArticleVO articleVO = new ArticleVO();
            BeanUtils.copyProperties(articleBO,articleVO);
            articleVOList.add(articleVO);
        });
        return Result.OK().setData(articleVOList);
    }

}
